package com.daoImpl;

import com.bean.Admin;
import com.bean.User;
import com.dao.AdminDAO;
import com.dao.BasicDAO;

import java.util.List;

/**
 * @author 徐洲
 * @version 1.0
 */
public class AdminDAOImpl extends BasicDAO implements AdminDAO {
    /**
     * 管理员登录
     */
    @Override
    public Integer adminLogin(String admin_name, String admin_password){
        String sql = "SELECT * FROM admin WHERE admin_name = ? AND admin_password = md5(?)";
        Admin user = (Admin) querySingle(sql, Admin.class, admin_name, admin_password);
        if (user == null) return -1;
        return user.getAdmin_id();
    }

    /**
     * 获取用户列表
     */
    @Override
    public List<User> getUserList(String keyword){
        String sql = "{call getByNameProc2(?)}";
        if (keyword == null) {
            keyword = "";
        }
        return queryMulti(sql, User.class, keyword);
    }

    /**
     * 删除用户
     */
    @Override
    public boolean deleteUser(String userid){
        String[] sqls = {"DELETE FROM collect WHERE user_id = ?",
                "DELETE FROM `comment` WHERE user_id = ?",
                "DELETE FROM article WHERE user_id = ?",
                "DELETE FROM users WHERE userid = ?"};
        return transactionUpdate(sqls, userid);
    }

    /**
     * 保存用户
     */
    @Override
    public boolean saveUser(User user){
        String sql = "insert into `users` (`username`, `password`, `sex`, `avatar`, `telephone`, `email`) values (?,md5(?),?,'img.png',?,?)";
        Integer integer = update(sql, user.getUsername(), user.getPassword(), user.getSex(), user.getTelephone(), user.getEmail());
        if (integer == null) return false;
        return  integer> 0;
    }

}
